<?php
	$ls_gridname	= 'dtl_sample2';


// ----------------- >> Form Detail << ----------------- ?>
<script type="text/javascript">

	function findDataInGrid(grid,label,val){
    	var rows = $(grid).datagrid('getData');
    	var jrow = $(grid).datagrid('getRows').length - 1;

		var rows2 = rows["rows"];

		for (var i = 0; i <= jrow; i++) {
			var rows3 = rows2[i];
			var kdb = rows3[label];
			var kdb2 = val;

			if(kdb == kdb2){
				return i;
			}
		};

		return null;
    }

    function getFieldByIndex(grid,index,label){
    	var rows = $(grid).datagrid('getData');
    	var jrow = $(grid).datagrid('getRows').length - 1;

		var rows2 = rows["rows"];
		var rows3 = rows2[index];
		var valb = rows3[label];

		return valb;
    }

	var grid_add = function() {
		var ls_error = '';
		var row	= formToJSON('form_<?php echo $ls_gridname; ?>');

		row.kd_barang = $('#kode_barang_add').val();
		row.nama_barang = $('#nama_barang').val();
		row.harga_barang = $('#harga_barang').val();
		row.jumlah = $('#jumlah_barang').val();
		row.discount_item = $('#discount_barang').val();
		row.sub_total = $('#sub_total_barang').val();

		if(row.kd_barang == ''){
			ls_error	+= 'Isi Barang\n';
		}

		if(row.jumlah == 0){
			ls_error	+= 'Isi Jumlah Barang\n';
		}

		if(ls_error!=''){
			$.messager.alert('Error', nl2br(ls_error), 'error');
		}else{
			var cekIndex = findDataInGrid('#<?php echo $ls_gridname; ?>', 'kd_barang', $('#kode_barang_add').val());

			if(cekIndex == null){
				$('#<?php echo $ls_gridname; ?>').datagrid('appendRow', row );

				if($('#total_bayar').val().length == 0){
					$('#total_bayar').setval(row.sub_total);
					$('#total_bayar_view').setval(row.sub_total);
				} else {
					var total_sebelum = parseInt($('#total_bayar').val());
					var total_baru = total_sebelum + parseInt(row.sub_total);
					$('#total_bayar').setval(total_baru);
					$('#total_bayar_view').setval(total_baru);
				}

			} else {
				var jumlah_old = parseInt(getFieldByIndex('#<?php echo $ls_gridname; ?>', cekIndex, 'jumlah'));
				var sub_total_old = parseInt(getFieldByIndex('#<?php echo $ls_gridname; ?>', cekIndex, 'sub_total'));
				var discount_item_old = getFieldByIndex('#<?php echo $ls_gridname; ?>', cekIndex, 'discount_item');

				if($('#discount_barang').val() != discount_item_old){
					$('#discount_barang').setval('0');
					ls_error	+= 'Diskon Item berbeda\n';
				}

				if(ls_error!=''){
					$.messager.alert('Error', nl2br(ls_error), 'error');
				}else{
					row.jumlah = jumlah_old + parseInt($('#jumlah_barang').val());
					row.discount_item = discount_item_old;
					row.sub_total = sub_total_old + parseInt($('#sub_total_barang').val());

					$('#<?php echo $ls_gridname; ?>').datagrid('updateRow',{index:cekIndex,row:row});

					var total_sebelum = parseInt($('#total_bayar').val());
					var total_baru = total_sebelum + parseInt($('#sub_total_barang').val());
					$('#total_bayar').setval(total_baru);
					$('#total_bayar_view').setval(total_baru);
					
				}
			}
		}

		set_null();
        
    };

    function set_null(){
    	$('#kode_barang_add').setval('');
		$('#nama_barang').setval('');
		$('#jumlah_barang').setval('');
		$('#harga_barang').setval('');
		$('#discount_barang').setval('0');
		$('#sub_total_barang').setval('');
		$('#kode_barang_add').focus();
    }

	$(document).ready(function(){
		// Setup Form
		$('#form_<?php echo $ls_gridname; ?>').setupForm([
			{ name: 'kode_penjualan_detail', type:'numeric', precision:0 },
			{ name: 'kd_barang', type: 'lov', size: 'short',
				onClearButton:function(){
					$(this).setval(''); 
					$('#nama_barang').setval(''); 
					$('#<?php echo $ls_gridname; ?>_nama_barang').setval('');
				},
				onActionButton:function(){ 
					LOVBarang({ onSelect:function(row){ 
						$('#<?php echo $ls_gridname; ?>_kd_barang').setval(row.kd_barang); 
						$('#<?php echo $ls_gridname; ?>_nama_barang').setval(row.nama_barang);
						$('#<?php echo $ls_gridname; ?>_harga_jual').setval(row.harga_jual); 
						$('#<?php echo $ls_gridname; ?>_jumlah').focus(); 
					} }); 
				}
			},
			{ name: 'nama_barang', type: 'text', size: '24', readonly:true },
			{ name: 'harga_jual', type: 'text', size: '24' },
			{ name: 'jumlah', type: 'text', size: '24' },
			{ name: 'discount_item', type: 'text', size: '24' },
			{ name: 'sub_total', type: 'text', precision:2, readonly:true }
		], {}, { prefix:'<?php echo $ls_gridname; ?>_' });
		
		// Set Hidden Object
		$('#<?php echo $ls_gridname; ?>_kode_penjualan_detail').hide();

		$('#<?php echo $ls_gridname; ?>_jumlah').keyup(function() {
			var jumlah = $('#<?php echo $ls_gridname; ?>_jumlah').val();
			var harga = $('#<?php echo $ls_gridname; ?>_harga_jual').val();
			var sub_total = jumlah * harga;
  			$('#<?php echo $ls_gridname; ?>_sub_total').setval(sub_total);
  			$('#<?php echo $ls_gridname; ?>_discount_item').setval(0);
		});

		$('#jumlah_barang').keyup(function() {
			var jumlah = $('#jumlah_barang').val();
			var harga = $('#harga_barang').val();
			var sub_total = jumlah * harga;
  			$('#sub_total_barang').setval(sub_total);
		});

		$('#discount_barang').keyup(function() {
			var diskon = $('#discount_barang').val();
			var jumlah = $('#jumlah_barang').val();
			var harga = $('#harga_barang').val();
			var sub_total = jumlah * harga - (jumlah * harga * (diskon / 100));
  			$('#sub_total_barang').setval(sub_total);
		});

		$('#jumlah_barang').keypress(function (e) {
		  if (e.which == 13) {
		    grid_add();
		    return false;
		  }
		  else if (e.which == 9) {
		    $('#discount_barang').setval('');
		    $('#discount_barang').focus();
		    return false;
		  }
		});
		
		$('#discount_barang').keypress(function (e) {
		  if (e.which == 13) {
		    grid_add();
		    return false;
		  }
		});

		$('#bayar').keypress(function (e) {
		  if (e.which == 13) {
		    $('#form_master').submitForm(	'/'+controller+'/ajax_post', 
			{ detail:[	{ table: 'dtl_sample2', field:['kode_penjualan_detail','kd_barang','jumlah','kode_penjualan','sub_total','discount_item']}																	], 
			//onComplete : function(data){ eval('oData = '+data); loadPage('/'+controller+'/edit/?kode_penjualan='+(oData.genid!='' ? oData.genid : $('#kode_penjualan').getval())); }
			onComplete : function(data){ eval('oData = '+data); loadPage('/'+controller+'/index'); }
			}); 
		    return false;
		  }
		});

	});
</script>

<?php 

// ----------------- >> Table Detail << ----------------- ?>
<script type="text/javascript">
	$(document).ready(function(){
		// var jrow = $('#<?php echo $ls_gridname; ?>').datagrid('getRows').length;
		// if(jrow != 0){
		// 	for(var i=0; i<jrow; i++){
		// 		$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow',i);
		// 	}
		// }

		$('#<?php echo $ls_gridname; ?>').datagrid({
			url:gs_path+'/'+controller+'/<?php echo $ls_gridname; ?>_data',
			queryParams:{ kode_penjualan:$('#kode_penjualan').getval(), rows:9999999999 },
			<?php if($mode!='view'){ 
			// -->> Toolbar [Start]
			// Only Appear On Mode Create / Edit ?>
			toolbar:[{
				text:'Hapus',
				iconCls:'icon-remove',
				handler:function(){
					var row = $('#<?php echo $ls_gridname; ?>').datagrid('getSelected');
					if(row==null){ $.messager.alert('&nbsp;', gs_msg_row_unselected, 'warning'); }
					else if(row){
						if(!confirm('Hapus baris data?')) return false;

						var index = $('#<?php echo $ls_gridname; ?>').datagrid('getRowIndex', row);
						var total_sebelum = parseInt($('#total_bayar').val());
						var total_baru = total_sebelum - parseInt(row.sub_total);

						$('#total_bayar').setval(total_baru);
						$('#total_bayar_view').setval(total_baru);

						$('#<?php echo $ls_gridname; ?>').datagrid('deleteRow', index);

						$('#kode_barang_add').focus();
					}
				}
			}],<?php 
			} // -->> Toolbar [End]
			?>
			onClickRow:function(rowIndex){
				var lastIndex = $('#<?php echo $ls_gridname; ?>').data('selectedIndex');
				$('#<?php echo $ls_gridname; ?>').data('selectedIndex', rowIndex);
			},
			onLoadSuccess:function(){ }
		});
		
	});
	
</script>

<form id="fm_add" onsubmit="grid_add(); return false;">
<table>
	<tr>
		<td>Kode Barang</td>
		<td>Nama Barang</td>
		<td>Harga</td>
		<td>Jumlah</td>
		<td>Discount (%)</td>
		<td>Sub Total</td>
	</tr>
	<tr>
		<td><input id="kode_barang_add"></td>
		<td><input id="nama_barang"></td>
		<td><input id="harga_barang"></td>
		<td><input id="jumlah_barang"></td>
		<td><input id="discount_barang"></td>
		<td><input id="sub_total_barang">
			<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="grid_add(); return false;">Add</a></td>
	</tr>
</table>
 
<br>
</form>

<table id="<?php echo $ls_gridname; ?>" rownumbers="true" style="width:600;height:auto" singleSelect="true" idField="kode_penjualan_detail" showFooter="true">
<thead>
    <tr>
    	<th field="kd_barang" width="200">Kode Barang</th>
        <th field="nama_barang" width="200">Nama Barang</th>
        <th field="harga_barang" width="120" align="right" formatter="formatNumericPoint2">Harga Barang</th>
        <th field="jumlah" width="120" align="right" formatter="formatNumericPoint2">Jumlah</th>
        <th field="discount_item" width="120" align="right" formatter="formatNumericPoint2">Discount Item (%)</th>
        <th field="sub_total" width="120" align="right" formatter="formatNumericPoint2">Sub Total</th>
    </tr>
</thead>
</table>
